﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ValidateCommon;
using Util.PageInfo;
using BusinessLogic.ClassFactory;
using System.Text;
using System.Xml;
using System.Diagnostics;
using DataAccess;
using Common;

namespace RichECMS.ServerPages.Links
{
    public partial class LinksList : Util.BasePage.BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request["Gid"] != null)
                {
                    if (ParamAuth.IsNumber(Request["Gid"].ToString()))
                    {
                        State.strWhereSql = "";
                        State.intPageIndex = 1;
                        State.intLinkGroupID = int.Parse(Request["Gid"].ToString());
                        this.btnAdd.HRef = this.btnAdd.HRef + "?Gid=" + State.intLinkGroupID.ToString();
                        this.btnSection.HRef = this.btnSection.HRef + "?Gid=" + State.intLinkGroupID.ToString();
                        InitDataBind(State.intPageIndex, AspNetPagerList.PageSize);
                        AspNetPagerList.CurrentPageIndex = State.intPageIndex;
                        LitPaagePath.Text = PageInfo.ShowLinkGroupName(State.intLinkGroupID);
                    }
                    else
                    {
                        Alert("进入此页面的方式不正确");
                        return;
                    }
                }
                else
                {
                    Alert("进入此页面的方式不正确");
                    return;
                }
            }
        }

        /// <summary>
        /// 绑定
        /// </summary>
        private void InitDataBind(int intCurrentPage, int intPageSize)
        {
            try
            {
                //绑定文章类型
                ListItem listitem = new ListItem("--请选择连接组--", "-1");
                DropDownListMainSectionlist.Items.Add(listitem);
                using (LinksGroupFactory objFactory = new LinksGroupFactory())
                {
                    BusinessLogic.BusinessRules.LinksGroup objLinksGroup = objFactory.Creator();
                    if (objLinksGroup != null)
                    {
                        for (int i = 0; i < objLinksGroup.Count; i++)
                        {
                            listitem = new ListItem(objLinksGroup.DataItem.chvGName, objLinksGroup.DataItem.intGroupID.ToString());
                            DropDownListMainSectionlist.Items.Add(listitem);
                            objLinksGroup.SelectRow(i + 1);
                        }
                    }
                }
                using (LinksInfoFactory objLinksInfoFactory = new LinksInfoFactory())
                {
                    StringBuilder strWhere = new StringBuilder();
                    int intPageFrom = intCurrentPage * intPageSize;
                    int intTotalCounts = 0;
                    LinksGroupFactory objLinksGroupFactory = new LinksGroupFactory();
                    BusinessLogic.BusinessRules.LinksGroup objLinksGroup = objLinksGroupFactory.Creator(State.intLinkGroupID);
                    BusinessLogic.BusinessRules.LinksInfo objLinksInfo = objLinksInfoFactory.NewInstance();
                    if (State.strWhereSql.Trim().Length == 0)
                    {
                        intTotalCounts = objLinksInfo.GetTotalCount("intGroupID=" + State.intLinkGroupID);
                    }
                    else
                    {
                        intTotalCounts = objLinksInfo.GetTotalCountByCustomSql("SELECT count(*) FROM (select Top " + (intPageFrom).ToString() + " intLinksID from LinksInfo " + " where intGroupID=" + State.intLinkGroupID.ToString() + State.strWhereSql + " order by intSort desc)t ");
                    }
                    //显示版块名称
                    LitPageName.Text = objLinksGroup.DataItem.chvGName;
                    LitPageCurrent.Text = intCurrentPage.ToString();
                    LitPageCount.Text = intTotalCounts.ToString();
                    if (intTotalCounts % intPageSize != 0)
                    {
                        if ((intCurrentPage == intTotalCounts / intPageSize + 1))
                        {
                            intPageFrom = (intCurrentPage - 1) * intPageSize;
                            //intPageSize =Model.Config.JudgeDatabase( intTotalCounts % intPageSize);//处理不同数据库差异
                            intPageSize = intTotalCounts % intPageSize;
                            intPageFrom = intPageFrom + intPageSize;
                        }
                    }
                    strWhere.Append("select * from (select top " + intPageSize + " * from  (select Top " + (intPageFrom).ToString() + " * from (select Top " + (intPageFrom).ToString() + " * from LinksInfo " + " where intGroupID=" + State.intLinkGroupID.ToString() + State.strWhereSql + " order by intSort desc ,intLinksID desc)t order by intSort asc, intLinksID asc)m " + " order by intSort asc,intLinksID asc)mm  order by intSort desc, intLinksID desc");
                    objLinksInfo = objLinksInfoFactory.CreatorByFullSql(strWhere.ToString());
                    if (objLinksInfo != null)
                    {
                        rptList.DataSource = objLinksInfo.Data.LinksInfo.DataSet.Tables[0].DefaultView;
                        AspNetPagerList.RecordCount = intTotalCounts;
                        rptList.DataBind();
                    }
                    else
                    {
                        rptList.DataSource = objLinksInfoFactory.NewInstance().Data.LinksInfo.DataSet.Tables[0].DefaultView;
                        AspNetPagerList.RecordCount = 0;
                        rptList.DataBind();
                        //Alert("暂无数据！");
                    }
                    objLinksGroupFactory.Dispose();
                }
            }
            catch (Exception ex)
            {
                Alert(ex.ToString().Replace("\r\n", " "));
            }
        }

        /// <summary>
        /// 搜索
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void ImageButtonsearch(object sender, ImageClickEventArgs e)
        {
            StringBuilder sbWhere = new StringBuilder();
            if (txtTitle.Value.Trim() != "")
            {
                sbWhere.AppendFormat(" and chvName like '%{0}%'", txtTitle.Value.Trim());
            }
            State.strWhereSql = sbWhere.ToString();
            State.intPageIndex = 1;
            InitDataBind(State.intPageIndex, AspNetPagerList.PageSize);
        }

        /// <summary>
        /// 排序
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSort_Click(object sender, ImageClickEventArgs e)
        {
            if (SortHidden.Value != "")
            {
                Response.Redirect("LinksSort.aspx?SortID=" + SortHidden.Value);
            }
            else
            {
                Alert("请选中要排序的文章！");
            }
        }

        /// <summary>
        /// 删除所选
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnDeleteAll_Click(object sender, ImageClickEventArgs e)
        {
            try
            {
                using (DBManager objDBManager = new DBManager())
                {
                    objDBManager.BeginTransaction();
                    try
                    {
                        LinksInfoFactory objLinksInfoFactory = new LinksInfoFactory(objDBManager);
                        objLinksInfoFactory.DeleteWhere(" intLinksID in (" + SortHidden.Value.Trim() + ")");

                        LinksGroupFactory objLinksGroupFactory = new LinksGroupFactory(objDBManager);
                        BusinessLogic.BusinessRules.LinksGroup objLinksGroup = objLinksGroupFactory.Creator(State.intLinkGroupID);
                        objLinksGroup.DataItem.intIssueState = (int)Utility.STIssueState.NoIssue;
                        objLinksGroup.Update();

                        //提交
                        objDBManager.Commit();
                        objLinksGroupFactory.Dispose();
                        objLinksInfoFactory.Dispose();
                        AlertAndRedirect("删除成功", "../links/LinksList.aspx?Gid=" + State.intLinkGroupID.ToString(), false);
                    }
                    catch (Exception ex)
                    {
                        Alert(ex.ToString().Replace("\r\n", " "));
                        objDBManager.Rollback();
                        return;
                    }
                }
            }
            catch (Exception ex)
            {
                Alert(ex.ToString().Replace("\r\n", " "));
            }
        }

        /// <summary>
        /// 批量放入回收站
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnRecovery_Click(object sender, ImageClickEventArgs e)
        {
        }

        /// <summary>
        /// 分页方法
        /// </summary>
        private void BindToPager()
        {
        }

        /// <summary>
        /// 分页  PageChanged
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void AspNetPagerArticle_PageChanged(object sender, EventArgs e)
        {
            State.intPageIndex = AspNetPagerList.CurrentPageIndex;
            InitDataBind(AspNetPagerList.CurrentPageIndex, AspNetPagerList.PageSize);
        }

        /// <summary>
        /// rptList_ItemCommand  编辑   放入回收站  删除
        /// </summary>
        /// <param name="source"></param>
        /// <param name="e"></param>
        protected void rptList_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "Delete")
            {
                using (DBManager objDBManager = new DBManager())
                {
                    int intLinksID = Convert.ToInt32(e.CommandArgument);
                    objDBManager.BeginTransaction();
                    try
                    {
                        LinksInfoFactory objLinksInfoFactory = new LinksInfoFactory(objDBManager);
                        objLinksInfoFactory.Delete(intLinksID);

                        LinksGroupFactory objLinksGroupFactory = new LinksGroupFactory(objDBManager);
                        BusinessLogic.BusinessRules.LinksGroup objLinksGroup = objLinksGroupFactory.Creator(State.intLinkGroupID);
                        objLinksGroup.DataItem.intIssueState = (int)Utility.STIssueState.NoIssue;
                        objLinksGroup.Update();

                        //提交
                        objDBManager.Commit();
                        objLinksGroupFactory.Dispose();
                        objLinksInfoFactory.Dispose();
                        AlertAndRedirect("删除成功", "../links/LinksList.aspx?Gid=" + State.intLinkGroupID.ToString(), false);
                    }
                    catch (Exception ex)
                    {
                        Alert(ex.ToString().Replace("\r\n", " "));
                        objDBManager.Rollback();
                        return;
                    }
                }
            }
            //放入回收站
            if (e.CommandName == "Recycle")
            {
            }
        }

        protected void BtnSubmitChangeSection_Click(object sender, EventArgs e)
        {
            if (ParamAuth.IsNumber(ChangeSecHidden.Value.Trim()))
            {
                int intShangeSec = Convert.ToInt32(ChangeSecHidden.Value.Trim());
                try
                {
                    using (DBManager objDBManager = new DBManager())
                    {
                        objDBManager.BeginTransaction();
                        try
                        {
                            //插入关联表
                            LinksInfoFactory objLinksInfoFactory = new LinksInfoFactory(objDBManager);
                            BusinessLogic.BusinessRules.LinksInfo objLinksInfo = objLinksInfoFactory.CreatorBySql(" intLinksID in (" + SortHidden.Value.Trim() + ")");
                            if (objLinksInfo != null)
                            {
                                for (int i = 0; i < objLinksInfo.Count; i++)
                                {
                                    objLinksInfo.DataItem.intGroupID = intShangeSec;
                                    objLinksInfo.SelectRow(i + 1);
                                }
                                objLinksInfo.Update();
                            }
                            //提交

                            LinksGroupFactory objLinksGroupFactory = new LinksGroupFactory(objDBManager);
                            BusinessLogic.BusinessRules.LinksGroup objLinksGroup = objLinksGroupFactory.Creator(State.intLinkGroupID);
                            objLinksGroup.DataItem.intIssueState = (int)Utility.STIssueState.NoIssue;
                            objLinksGroup.Update();


                            objLinksGroupFactory = new LinksGroupFactory(objDBManager);
                            objLinksGroup = objLinksGroupFactory.Creator(intShangeSec);
                            objLinksGroup.DataItem.intIssueState = (int)Utility.STIssueState.NoIssue;
                            objLinksGroup.Update();

                            objDBManager.Commit();
                            objLinksGroupFactory.Dispose();
                            objLinksInfoFactory.Dispose();
                            AlertAndRedirect("所选条目已经迁移到指定板块", "../links/LinksList.aspx?Gid=" + State.intLinkGroupID.ToString(), false);
                        }
                        catch (Exception ex)
                        {
                            Alert(ex.ToString().Replace("\r\n", " "));
                            objDBManager.Rollback();
                            return;
                        }
                    }
                }
                catch (Exception ex)
                {
                    Alert(ex.ToString().Replace("\r\n", " "));
                }
            }
            else
            {
                Alert("参数错误");
            }
        }
    }
}